#include "customizecolumndialog.h"
#include "ui_customizecolumndialog.h"
#include "wlistpatient.h"

#include <QTableView>

CustomizeColumnDialog::CustomizeColumnDialog(QTableView *view, QWidget *parent) :
    tableView(view),
    QDialog(parent),
    ui(new Ui::CustomizeColumnDialog)
{
    ui->setupUi(this);

    connect(ui->defaultButton, SIGNAL(clicked()), this, SLOT(onDefault()));

    init();
}

CustomizeColumnDialog::~CustomizeColumnDialog()
{
    delete ui;
}

void CustomizeColumnDialog::init()
{
    ui->accNumberCheck->setChecked(settings.value(ACCNUMBER_COL, 1).toBool());
    ui->patientIDCheck->setChecked(settings.value(PATIENTID_COL, 1).toBool());
    ui->patientNameCheck->setChecked(settings.value(PATIENTNAME_COL, 1).toBool());
    ui->patientSexCheck->setChecked(settings.value(PATIENTSEX_COL, 1).toBool());
    ui->patientBirthdateCheck->setChecked(settings.value(PATIENTBIRTH_COL, 1).toBool());
    ui->patientAgeCheck->setChecked(settings.value(PATIENTAGE_COL, 1).toBool());
    ui->reqPhysicianCheck->setChecked(settings.value(REQPHYSICIAN_COL, 1).toBool());
    ui->reqProcIDCheck->setChecked(settings.value(REQPROCID_COL, 1).toBool());
    ui->reqProcDescCheck->setChecked(settings.value(REQPROCDESC_COL, 1).toBool());
    ui->reqProcPrioCheck->setChecked(settings.value(REQPROCPRIO_COL, 1).toBool());
    ui->modalityCheck->setChecked(settings.value(MODALITY_COL, 1).toBool());
    ui->schPhysicianCheck->setChecked(settings.value(SCHPHYSICIAN_COL, 1).toBool());
    ui->schStationAECheck->setChecked(settings.value(SCHSTATIONAE_COL, 1).toBool());
    ui->schStationNameCheck->setChecked(settings.value(SCHSTATIONNAME_COL, 1).toBool());
    ui->schDateTimeCheck->setChecked(settings.value(SCHDATETIME_COL, 1).toBool());
    ui->schProcStepDescCheck->setChecked(settings.value(SCHPROCSTEPDESC_COL, 1).toBool());
    ui->schProcStepIDCheck->setChecked(settings.value(SCHPROCSTEPID_COL, 1).toBool());

    ui->admissionIDCheck->setChecked(settings.value(ADMISSIONID_COL, 1).toBool());
    ui->medicalAlertsCheck->setChecked(settings.value(MEDICALALERTS_COL, 1).toBool());
    ui->deparmentCheck->setChecked(settings.value(DEPARTMENT_COL, 1).toBool());
    ui->patientSizeCheck->setChecked(settings.value(PATIENTSIZE_COL, 0).toBool());
    ui->patientWeightCheck->setChecked(settings.value(PATIENTWEIGHT_COL, 0).toBool());
    ui->patientPhoneCheck->setChecked(settings.value(PATIENTPHONE_COL, 0).toBool());
    ui->patientAddrCheck->setChecked(settings.value(PATIENTADDR_COL, 0).toBool());

    ui->creationTimeCheck->setChecked(settings.value(CREATIONTIME_COL, 1).toBool());
    ui->lastModifiedTimeCheck->setChecked(settings.value(MODIFICATIONTIME_COL, 1).toBool());

    connect(ui->accNumberCheck, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
    connect(ui->patientIDCheck, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
    connect(ui->patientNameCheck, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
    connect(ui->patientSexCheck, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
    connect(ui->patientBirthdateCheck, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
    connect(ui->patientAgeCheck, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
    connect(ui->reqPhysicianCheck, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
    connect(ui->reqProcIDCheck, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
    connect(ui->reqProcDescCheck, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
    connect(ui->reqProcPrioCheck, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
    connect(ui->modalityCheck, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
    connect(ui->schPhysicianCheck, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
    connect(ui->schStationAECheck, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
    connect(ui->schStationNameCheck, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
    connect(ui->schDateTimeCheck, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
    connect(ui->schProcStepDescCheck, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
    connect(ui->schProcStepIDCheck, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
    connect(ui->admissionIDCheck, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
    connect(ui->medicalAlertsCheck, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
    connect(ui->deparmentCheck, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
    connect(ui->patientSizeCheck, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
    connect(ui->patientWeightCheck, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
    connect(ui->patientPhoneCheck, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
    connect(ui->patientAddrCheck, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
    connect(ui->creationTimeCheck, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
    connect(ui->lastModifiedTimeCheck, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
}

void CustomizeColumnDialog::onStateChanged(int state)
{
    QCheckBox *colWidget = static_cast<QCheckBox*>(sender());

    if (colWidget == ui->accNumberCheck) {
        tableView->setColumnHidden(WListPatient::AccNumber, !state);
        settings.setValue(ACCNUMBER_COL, state);
    } else if (colWidget == ui->patientIDCheck) {
        tableView->setColumnHidden(WListPatient::PatientID, !state);
        settings.setValue(PATIENTID_COL, state);
    } else if (colWidget == ui->patientNameCheck) {
        tableView->setColumnHidden(WListPatient::PatientName, !state);
        settings.setValue(PATIENTNAME_COL, state);
    } else if (colWidget == ui->patientSexCheck) {
        tableView->setColumnHidden(WListPatient::PatientSex, !state);
        settings.setValue(PATIENTSEX_COL, state);
    } else if (colWidget == ui->patientBirthdateCheck) {
        tableView->setColumnHidden(WListPatient::PatientBirth, !state);
        settings.setValue(PATIENTBIRTH_COL, state);
    } else if (colWidget == ui->patientAgeCheck) {
        tableView->setColumnHidden(WListPatient::PatientAge, !state);
        settings.setValue(PATIENTAGE_COL, state);
    } else if (colWidget == ui->reqPhysicianCheck) {
        tableView->setColumnHidden(WListPatient::ReqPhysician, !state);
        settings.setValue(REQPHYSICIAN_COL, state);
    } else if (colWidget == ui->medicalAlertsCheck) {
        tableView->setColumnHidden(WListPatient::MedicalAlerts, !state);
        settings.setValue(MEDICALALERTS_COL, state);
    } else if (colWidget == ui->reqProcIDCheck) {
        tableView->setColumnHidden(WListPatient::ReqProcID, !state);
        settings.setValue(REQPROCID_COL, state);
    } else if (colWidget == ui->reqProcDescCheck) {
        tableView->setColumnHidden(WListPatient::ReqProcDesc, !state);
        settings.setValue(REQPROCDESC_COL, state);
    } else if (colWidget == ui->reqProcPrioCheck) {
        tableView->setColumnHidden(WListPatient::ReqProcPrio, !state);
        settings.setValue(REQPROCPRIO_COL, state);
    } else if (colWidget == ui->modalityCheck) {
        tableView->setColumnHidden(WListPatient::Modality, !state);
        settings.setValue(MODALITY_COL, state);
    } else if (colWidget == ui->schPhysicianCheck) {
        tableView->setColumnHidden(WListPatient::SchPhysician, !state);
        settings.setValue(SCHPHYSICIAN_COL, state);
    } else if (colWidget == ui->schStationAECheck) {
        tableView->setColumnHidden(WListPatient::SchStationAE, !state);
        settings.setValue(SCHSTATIONAE_COL, state);
    } else if (colWidget == ui->schStationNameCheck) {
        tableView->setColumnHidden(WListPatient::SchStationName, !state);
        settings.setValue(SCHSTATIONNAME_COL, state);
    } else if (colWidget == ui->schDateTimeCheck) {
        tableView->setColumnHidden(WListPatient::SchDateTime, !state);
        settings.setValue(SCHDATETIME_COL, state);
    } else if (colWidget == ui->schProcStepDescCheck) {
        tableView->setColumnHidden(WListPatient::SchProcStepDesc, !state);
        settings.setValue(SCHPROCSTEPDESC_COL, state);
    } else if (colWidget == ui->schProcStepIDCheck) {
        tableView->setColumnHidden(WListPatient::SchProcStepID, !state);
        settings.setValue(SCHPROCSTEPID_COL, state);
    } else if (colWidget == ui->admissionIDCheck) {
        tableView->setColumnHidden(WListPatient::AdmissionID, !state);
        settings.setValue(ADMISSIONID_COL, state);
    } else if (colWidget == ui->deparmentCheck) {
        tableView->setColumnHidden(WListPatient::Department, !state);
        settings.setValue(DEPARTMENT_COL, state);
    } else if (colWidget == ui->patientSizeCheck) {
        tableView->setColumnHidden(WListPatient::PatientSize, !state);
        settings.setValue(PATIENTSIZE_COL, state);
    } else if (colWidget == ui->patientWeightCheck) {
        tableView->setColumnHidden(WListPatient::PatientWeight, !state);
        settings.setValue(PATIENTWEIGHT_COL, state);
    } else if (colWidget == ui->patientPhoneCheck) {
        tableView->setColumnHidden(WListPatient::PatientPhone, !state);
        settings.setValue(PATIENTPHONE_COL, state);
    } else if (colWidget == ui->patientAddrCheck) {
        tableView->setColumnHidden(WListPatient::PatientAddress, !state);
        settings.setValue(PATIENTADDR_COL, state);
    } else if (colWidget == ui->creationTimeCheck) {
        tableView->setColumnHidden(WListPatient::CreationDateTime, !state);
        settings.setValue(CREATIONTIME_COL, state);
    } else if (colWidget == ui->lastModifiedTimeCheck) {
        tableView->setColumnHidden(WListPatient::LastModifiedDateTime, !state);
        settings.setValue(MODIFICATIONTIME_COL, state);
    }
}

void CustomizeColumnDialog::onDefault()
{
    ui->accNumberCheck->setChecked(1);
    ui->patientIDCheck->setChecked(1);
    ui->patientNameCheck->setChecked(1);
    ui->patientSexCheck->setChecked(1);
    ui->patientBirthdateCheck->setChecked(1);
    ui->reqPhysicianCheck->setChecked(1);
    ui->reqProcIDCheck->setChecked(1);
    ui->reqProcDescCheck->setChecked(1);
    ui->reqProcPrioCheck->setChecked(1);
    ui->modalityCheck->setChecked(1);
    ui->schPhysicianCheck->setChecked(1);
    ui->schStationAECheck->setChecked(1);
    ui->schStationNameCheck->setChecked(1);
    ui->schDateTimeCheck->setChecked(1);
    ui->schProcStepDescCheck->setChecked(1);
    ui->schProcStepIDCheck->setChecked(1);

    ui->admissionIDCheck->setChecked(1);
    ui->deparmentCheck->setChecked(1);
    ui->patientSizeCheck->setChecked(0);
    ui->patientWeightCheck->setChecked(0);
    ui->patientPhoneCheck->setChecked(0);
    ui->patientAddrCheck->setChecked(0);

    ui->creationTimeCheck->setChecked(1);
    ui->lastModifiedTimeCheck->setChecked(1);
}
